package com.collection.web.controller;


import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.collection.api.ApplyAuditService;
import com.collection.api.ApplyMainService;
import com.collection.api.UBaseService;
import com.collection.api.entity.ApplyAudit;
import com.collection.api.entity.ApplyMain;
import com.collection.api.entity.UBase;
import com.collection.dto.RequestDTO;
import com.collection.dto.ResponseDTO;
import com.collection.util.MathUtils;
import com.collection.util.MediaTypes;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;

/**
  * @Description :接口进件controller
  * @author : zongzp
  * @date :2017年11月7日上午9:15:16
  * @version : 1.1.0
  */
@Controller
@RequestMapping("/pieces")
@Api(value="进件")
public class IntoPiecesController {
	
	private static Logger logger = LoggerFactory.getLogger(IntoPiecesController.class);
	
	@Reference(version="1.0.0")
	private ApplyMainService  applyMainService;
	
	@Reference(version="1.0.0")
	private UBaseService  uBaseService;
	
	@Reference(version="1.0.0")
	private ApplyAuditService applyAuditService;
	
	
	@ApiOperation(value="获取进件接口",notes="此接口作用是获取app提交用户活动申请，每完成一个活动，提交一次记录到审批系统")
	@ApiImplicitParam(name="DTO",value="统一请求入参DTO，请求数据在data中，必须有{idno:'身份证号码'</br>,isAuto:'是否需要自动通过（1:是;0:否）'"
			+ "</br>,applyId:'用户申请id,'</br>amount:'用户申请额度',</br>createTime:'用户申请时间',</br>platform:'设备',"
			+ "</br>phone:'手机号',</br>card_number:'银行卡号',</br>name:'用户姓名'}",required=true,dataType="RequestDTO")
	@RequestMapping(value="/getIntoPieces",method = RequestMethod.POST,produces=MediaTypes.JSON_UTF_8)
	@ResponseBody
	public ResponseDTO getIntoPieces(@RequestBody RequestDTO paramStr){
		ResponseDTO responseDTO  = new ResponseDTO(null, null);
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
		JSONObject pieceJson = JSONObject.fromObject(paramStr);
		logger.info("进件获取到的参数为：==={}",pieceJson);
		try {
			JSONObject dataJson = (JSONObject)pieceJson.get("data");
			String idno = (String)dataJson.get("idno");
			String isAuto = (String)dataJson.get("isAuto");//是否需要自动通过审批1：是  0：否
			Integer applyId = (Integer)dataJson.get("applyId");
			Object amoun= dataJson.get("amount");
			BigDecimal amount = MathUtils.getBigDecimal(amoun);
			String createTime = (String)dataJson.get("createTime");
			Integer platform = (Integer)dataJson.get("platform");
			String phone = (String)dataJson.get("phone");
			String cardNumber = (String)dataJson.get("card_number");
			String name = (String)dataJson.get("name");
			ApplyMain applyMain = new ApplyMain();
			applyMain.setAmount(amount);
			applyMain.setApplyMainId(applyId);
			applyMain.setCreateTime(sFormat.parse(createTime));
			applyMain.setIdNo(idno);
			applyMain.setPlatform(platform);
			//0：未提交 1：审核中   2 ：通过 3：未通过 能修改 4：未通过（拒绝 不支持修改）5：完成还款 6：已提交待分配；7.已经分配；20:激活成功
			if(isAuto.equals("1")){
				applyMain.setStatus(2);
			}else{
				applyMain.setStatus(6);
			}
			
			int applyResult = applyMainService.insert(applyMain);//保存到apply_main表
			UBase uBase = new UBase();
			
			uBase.setIdNo(idno);
			uBase.setName(name);
			uBase.setPhone(phone);
			uBase.setBankCardNo(cardNumber);
			int baseResult = uBaseService.insert(uBase);//保存到u_base表
			//保存到审批记录表
			ApplyAudit applyAudit = new ApplyAudit();
			applyAudit.setApplyMainId(applyId);
			applyAudit.setCreateTime(new Date());
			applyAudit.setStatus(1);
			int applyAuditResult = applyAuditService.insert(applyAudit);
			
			Map<String, Object> resultMap = new HashMap<String, Object>();
			String success ="";
			if(applyResult>0 && baseResult>0 && applyAuditResult>0){
				success="true";
			}else {
				success="false";
			}
			resultMap.put("success", success);
			responseDTO.setResults(resultMap);
		} catch (ParseException e) {
			logger.info("获取进件异常====={}======",e);
			e.printStackTrace();
			responseDTO = new ResponseDTO(null, 400, e.getMessage());
		}
		return responseDTO;
	}
}

